﻿package cn.poco.pepsi;


import cn.poco.pepsi.activity.detail.CommentListActivity;
import cn.poco.pepsi.activity.detail.FoodPhotoListActivity;
import cn.poco.pepsi.activity.detail.ResDetailActivity_x;
import cn.poco.pepsi.activity.onhere.OnHereRecoreActivity;
import cn.poco.pepsi.bean.ResDetail;
import cn.poco.pepsi.bean.Restaurant;
import cn.poco.pepsi.service.ISearch;
import cn.poco.pepsi.service.impl.SearchImpl;
import cn.poco.pepsi.util.Cons;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TextView;
import android.widget.TableLayout.LayoutParams;

/**
 * @author Administrator
 *每个页签对应的Activity
 */
public class ResTabActivity extends android.app.TabActivity {
	
	private Handler tHandler;
	private ResDetail resD=null;
	private String res_code="";
	private TextView resName_textView;
	private ImageView poco_v,pepsi_v,discut_v;
	private Intent All_intent;
	private Bundle bundle;
	private LinearLayout progressLayout;
	
	public static TabHost tabs;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.setContentView(R.layout.sub_tab_ps);
		initView();
		tabs = getTabHost(); 
		
	    
	   /** 餐厅信息参数 **/
		All_intent = getIntent();
		res_code=All_intent.getStringExtra(Cons.RES_CODE);
		bundle=getIntent().getExtras();
		
		
		
		
		HandlerThread ht = new HandlerThread("XW");
		ht.start();
		tHandler = new Handler(ht.getLooper());
		tHandler.post(runnable);
		
	
		
		
	 
			
				
				
				
        
     		// 动态切换图片及文字  未完成
		tabs.setOnTabChangedListener(new OnTabChangeListener() {
			@Override
			public void onTabChanged(String tabId) {
				
			}
		});
        
        

        
}
	
	public void initView(){
		resName_textView=(TextView) findViewById(R.id.res_name_restab);
		poco_v=(ImageView) findViewById(R.id.poco_v);
		pepsi_v=(ImageView) findViewById(R.id.pepsi_v);
		discut_v=(ImageView) findViewById(R.id.discut_v);
		progressLayout = (LinearLayout) findViewById(R.id.progressbar);
	}
	
	public  void addTabs(){
		   //资料Tab
     TabHost.TabSpec resTab = tabs.newTabSpec("resTab");
     if(resD!=null){
     	Intent resIntent = new Intent(this,ResDetailActivity_x.class);//ResDetailActivity.class
     	resIntent.putExtra(Cons.RESDATA, resD);
         resIntent.putExtra(Cons.RES_CODE,res_code);
         resTab.setContent(resIntent);
     
     
     resTab.setIndicator(composeLayout("资料",Color.BLACK,1));
     tabs.addTab(resTab);
     }
     
     //美食美图
     if(true){
     	 TabHost.TabSpec connentTab = tabs.newTabSpec("connentTab");
          Intent photoIntent = new Intent(this, FoodPhotoListActivity.class);
          photoIntent.putExtras(bundle);//bundle只有餐厅的id
          connentTab.setContent(photoIntent);
          connentTab.setIndicator(composeLayout("美食美图",Color.BLACK,2));
          tabs.addTab(connentTab);
     }
     
     
     
    //食评  
     if(true){
     	 TabHost.TabSpec preTab = tabs.newTabSpec("preTab");
          Intent preIntent = new Intent(this, CommentListActivity.class);
          //发ID过去    未完成
          preTab.setContent(preIntent);
          preTab.setIndicator(composeLayout("食评",Color.BLACK,3));
          tabs.addTab(preTab);
     }
    
     //签到Tab  
     if(true){
         TabHost.TabSpec imgTab = tabs.newTabSpec("imgTab");
         Intent imgIntent = new Intent(this, OnHereRecoreActivity.class);
        imgIntent.putExtras(bundle);//bundle只有餐厅的id
         imgTab.setContent(imgIntent);
         imgTab.setIndicator(composeLayout("签到",Color.BLACK,4));
         tabs.addTab(imgTab);
     }
     
	}

	Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				
				resName_textView.setText(resD.getTitle());
				progressLayout.setVisibility(View.GONE);
				whichjoin();
				addTabs();
				break;
			}
		}
	};
	
Runnable runnable=new Runnable() {
		
		@Override
		public void run() {
			try {
				ISearch impl=new SearchImpl(ResTabActivity.this);
				resD=impl.getResDetail(res_code);
				handler.sendEmptyMessage(1);
				
			}catch(Exception e){
				}
			}
		};
			
		
		
		
	public void whichjoin(){
		if(resD.getVouch()==1){
			poco_v.setImageResource(R.drawable.join);
		}
		if(resD.getPepsi()==1){
			pepsi_v.setImageResource(R.drawable.pepsi);
		}
		if(resD.getDiscount()==1){
			discut_v.setImageResource(R.drawable.discount);
		}
	}
	
	
	//自定义布局
	public View composeLayout(String s,int color,int n){
		   LinearLayout layout = new LinearLayout(this);
		   layout.setOrientation(LinearLayout.VERTICAL);
		   layout.setGravity(Gravity.CENTER);
		   TextView textview = new TextView(this);
		   textview.setText(s);
		   textview.setTag("title");
		   textview.setTextColor(color);
		   textview.setTextSize(16f);
		   textview.setGravity(Gravity.CENTER);
		   layout.addView(textview, new LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT,
				   android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
		    RelativeLayout  relative = new RelativeLayout(this);
		    relative.setGravity(Gravity.CENTER);
			relative.addView(layout, new LayoutParams(
    				android.view.ViewGroup.LayoutParams.FILL_PARENT,android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
			
			//给标签
			switch (n) {
			case 1:
				relative.setBackgroundResource(R.drawable.res_tab1);
				break;
            case 2:
            	relative.setBackgroundResource(R.drawable.res_tab2);
				break;
            case 3:
            	relative.setBackgroundResource(R.drawable.res_tab3);
	            break;
            case 4:
            	relative.setBackgroundResource(R.drawable.res_tab4);
	            break;

			default:
				break;
			}
    		
		return relative;
	}
	


	
	 
}
